package uns.fit.gis.fileio;

import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.util.Enumeration;
import java.util.Vector;

import javax.microedition.io.Connector;
import javax.microedition.io.file.FileConnection;

import uns.fit.gis.MapDrawer;
import uns.fit.gis.Util;
import uns.fit.gis.mapdrawer.LayerConfig;
import uns.fit.gis.mapdrawer.MapCanvas;
import uns.fit.gis.mapobj.SimpleLocation;

public class FileWrapper {
	private static FileConnection getConnection(String connectionString) {
		FileConnection fc = null;
		try {
			fc = (FileConnection)Connector.open(connectionString, Connector.READ_WRITE);
		}
		catch(IOException exception) {
			exception.printStackTrace();
		}
		return fc;
	}
	
	public static FileConnection delete(String connectionString) {
		FileConnection fc = null;
		try {
			fc = (FileConnection)Connector.open(connectionString);
			if(fc.exists()) {
				fc.setWritable(true);
				fc.delete();
				fc.close();
			}
		}
		catch(IOException exception) {
			exception.printStackTrace();
		}
		return fc;
	}
	
	public static boolean exists(String fileName) {
		FileConnection fc = getConnection(fileName);
		return fc.exists();
	}
	
	public static void save(IUserData userData, String fileName) {
		try {
			FileConnection fc = getConnection(fileName);
			if(!fc.exists()) {
				fc.create();
			}
			userData.save(fc);
			fc.close();
		}
		catch(IOException exception) {
			exception.printStackTrace();
		}
	}
	
	public static void load(IUserData userData, String fileName) {
		FileConnection fc = getConnection(fileName);
		if(fc.exists()) {
			try {
				userData.load(fc);
				fc.close();
			}
			catch(IOException ioe) {
				ioe.printStackTrace();
			}
		}
	}

	public static byte[] load(String fileName) {
		FileConnection fc = getConnection(fileName);
		byte[] bytes = null;
		if(fc.exists()) {
			try {
				InputStream in = fc.openInputStream();
				int size = (int)fc.fileSize();
				bytes = new byte[size];
				in.read(bytes);
				in.close();
				fc.close();
			}
			catch(IOException exception) {
				exception.printStackTrace();
			}
		}
		return bytes;
	}
	
	public static void save(byte[] bytes, String fileName) {
		FileConnection fc = getConnection(fileName);
		try {
			if(!fc.exists()) {
				fc.create();
			}
			OutputStream out = fc.openOutputStream();
			out.write(bytes);
			out.close();
			fc.close();
		}
		catch(IOException exception) {
			exception.printStackTrace();
		}
	}
	
	public static void save(String str, String fileName) {
		FileConnection fc = getConnection(fileName);
		try {
			if(!fc.exists()) {
				fc.create();
			}
			System.out.println(fc.getName());
			DataOutputStream out = fc.openDataOutputStream();
			out.writeUTF(str);
			System.out.println(str);
			out.close();
			fc.close();
		}
		catch(IOException exception) {
			exception.printStackTrace();
		}
	}
	
	public static Enumeration list(String folderName, String filter) {
		Enumeration enumeration = null;
		FileConnection fc = getConnection(folderName);
		try {
			enumeration = fc.list(filter, true);
		}
		catch(IOException exception) {
			exception.printStackTrace();
		}
		return enumeration;
	}
	
	public static void saveUserLocation(String fileName, String note, int x, int y) {
		FileConnection fc = getConnection(fileName);
		try {
			if(!fc.exists()) {
				fc.create();
			}
			OutputStream outputStream = fc.openOutputStream(fc.fileSize());
			DataOutputStream out = new DataOutputStream(outputStream);
			out.writeInt(x);
			out.writeInt(y);
			out.writeUTF(note);
			out.close();
			outputStream.close();
			fc.close();
		}
		catch(IOException ioe) {
			ioe.printStackTrace();
		}
	}
	
	public static Vector loadUserLocations(String fileName) {
		FileConnection fc = getConnection(fileName);
		Vector locations = new Vector();
		try {
			if(fc.exists()) {
				fc.setReadable(true);
				DataInputStream in = fc.openDataInputStream();
				int n = MapDrawer.getMidlet().getSettings().getNumberOfUserLocations();
				System.out.println(n);
				for(int i = 0; i < n; i ++) {
					SimpleLocation location = new SimpleLocation();
					/*
					 * In fact, setting id is not important 
					 */
					location.setId(i);
					location.setType(LayerConfig.LAYER_OTHER_LOCATION);
					location.setX(in.readInt());
					location.setY(in.readInt());
					location.setName(Util.encodeStringToBytes(in.readUTF()));
					locations.addElement(location);
				}
				in.close();
				fc.close();
			}
		}
		
		catch (IOException ioe) {
			ioe.printStackTrace();
		}
		return locations;
	}
	
	public static void saveUserLocations(String fileName, Vector locations) {
		delete(fileName);
		FileConnection fc = getConnection(fileName);
		try {
			fc.create();
			DataOutputStream out = fc.openDataOutputStream();
			Enumeration locationEnum = MapCanvas.userLocations.elements();
	        while (locationEnum.hasMoreElements() == true) {
	            SimpleLocation location = (SimpleLocation)locationEnum.nextElement();
	            out.writeInt(location.getX());
	            out.writeInt(location.getY());
	            String str = Util.decodeCharacters(location.getName());
	            System.out.println(str);
	            out.writeUTF(str);
	        }
			out.close();
			fc.close();
		}
		catch(IOException ioe) {
			ioe.printStackTrace();
		}
	}
}
